<?php
/**
 * Copyright (C) 2013 Pablo Sepúlveda P. <psep.pentauc@gmail.com>
 * 
 * This file is part of gAbstract.
 * WarpIT is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * gAbstract is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with gAbstract.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * Clase abstracta para la instancia de conexión.
 * 
 * @author psep
 * @version 1.0
 * @package gAbstract
 * @copyright Copyright (c) 2013 Pablo Sepúlveda P.
 * @license GNU GPLv3 or later
 * @link http://www.psep.cl
 */
abstract class AbstractConnect {

	/**
	 * Valor del host de la base de datos.
	 * 
	 * @var String object.
	 */
	protected $host;
	
	/**
	 * Valor del puerto de la base de datos.
	 * 
	 * @var String object.
	 */
	protected $port;
	
	/**
	 * Valor del nombre de la base de datos.
	 * 
	 * @var String object.
	 */
	protected $db;
	
	/**
	 * Valor del username de la base de datos.
	 * 
	 * @var String object
	 */
	protected $user;
	
	/**
	 * Valor de la password de la base de datos.
	 * 
	 * @var String object.
	 */
	protected $password;
	
	/**
	 * Valor para la elección de base de datos.
	 * 
	 * @var Integer
	 */
	protected $driver;
	
	/**
	 * Método que retorna un objeto de conexión según la elección
	 * del driver PDO, éste es según parámetro entero
	 * en el atributo privado $driver.
	 * 
	 * @return PDO::Connection object
	 */
	protected function loadConnection(){
		switch ($this->driver) {
			case 1:
				return $this->getMySQL();
				break;
				
			case 2:
				return $this->getPostgreSQL();
				break;
			
			case 3:
				return $this->getSQLite();
				break;
			
			default:
				throw new Exception("Error: Debe seleccionar una db correcta", 1);
				break;
		}
		
	}
	
	/**
	 * Método privado que retorna una instancia de conexión
	 * PDO para base de datos PostgreSQL.
	 * 
	 * @return PDO::Connection Object
	 */
	private function getPostgreSQL(){
		$con = NULL;
		
		try{
			$driver = new StringBuilder();
			$driver->append("pgsql:dbname=");
			$driver->append($this->db);
			$driver->append(";host=");
			$driver->append($this->host);
			$driver->append(";user=");
			$driver->append($this->user);
			$driver->append(";password=");
			$driver->append($this->password);

			$con = new PDO($driver->toString());
			$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		}catch(PDOException $e){
			echo $e->getMessage();
		}
			
		return $con;
	}
	
	/**
	 * Método privado que retorna una instancia de conexión
	 * PDO para base de datos MySQL.
	 * 
	 * @return PDO::Connection Object
	 */
	private function getMySQL(){
		$con = NULL;
		
		try{
			$driver = new StringBuilder();
			$driver->append("mysql:host=");
			$driver->append($this->host);
			$driver->append(";port=");
			$driver->append($this->port);
			$driver->append(";dbname=");
			$driver->append($this->db);

			$con = new PDO($driver->toString(), $this->user, $this->password);
			$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$con->exec("SET NAMES 'utf8'");
		}catch(PDOException $e){
			echo $e->getMessage();
		}
			
		return $con;
	}
	
	/**
	 * Método privado que retorna una instancia de conexión
	 * PDO para base de datos SQLite.
	 * 
	 * @return PDO::Connection Object
	 */
	private function getSQLite(){
		$con = NULL;
		
		try{
			$driver = new StringBuilder();
			$driver->append("sqlite:");
			$driver->append($this->db);

			$con = new PDO($driver->toString());
			$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		}catch(PDOException $e){
			echo $e->getMessage();
		}
			
		return $con;
	}
	
	/**
	 * Método que cierra conexión y statement de PDO.
	 * 
	 * @param PDO::Connection Object.
	 * @param PDO::Statement Object.
	 */	
	protected function closeAll($con, $stm){
		$con = null;
		$stm = null;
	}

}

?>